//
//  editAudioWave.m
//  SoundPill
//
//  Created by 宋旭东®Des on 15/1/28.
//  Copyright (c) 2015年 宋旭东. All rights reserved.
//

#import "editAudioWave.h"

@interface editAudioWave ()
{
    float progre;
}
@property (strong, nonatomic) UIButton *btn;
@property (strong, nonatomic) NSArray *waveArray;
@property (assign, nonatomic) NSInteger arrayIndex;
@property (assign, nonatomic) NSInteger arrayIndexPlay;
@property (assign, nonatomic) BOOL state;
@end

@implementation editAudioWave
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.editAudioWaveAll = [[editAuidoWave_all alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        _editAudioWaveAll.backgroundColor = [UIColor clearColor];
        [self addSubview:_editAudioWaveAll];
        self.waveArray = [[NSArray alloc] init];
    }
    return self;
}

- (void)getAudioWaveArray:(NSArray *)array
{
    _waveArray = array;
}

- (void)getAudioProgress:(float)progress
{
    _arrayIndex = [_waveArray count] * progress;
    [self setNeedsDisplay];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self];
    //touchPoint.x ，touchPoint.y 就是触点的坐标
    if (touchPoint.x >= 0 && touchPoint.x < self.frame.size.width) {
        
        progre = touchPoint.x / self.bounds.size.width;
        NSInteger arrayCount = [_waveArray count];
        _arrayIndex = arrayCount * progre;
        [self setNeedsDisplay];
    }
    NSNumber *num = [NSNumber numberWithFloat:progre];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pause" object:num];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"audioProgress" object:num];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self];
    //touchPoint.x ，touchPoint.y 就是触点的坐标
    if (touchPoint.x >= 0 && touchPoint.x < self.frame.size.width) {
        progre = touchPoint.x / self.bounds.size.width;
        NSInteger arrayCount = [_waveArray count];
        _arrayIndex = arrayCount * progre;
        [self setNeedsDisplay];
    }
    NSNumber *num = [NSNumber numberWithFloat:progre];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pause" object:num];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"audioProgress" object:num];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//    NSNumber *num = [NSNumber numberWithFloat:progress];
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"audioProgress" object:num];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    CGFloat offset = 0;
    self.offsetNum = [NSNumber numberWithFloat:offset];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineCap(context, kCGLineCapSquare); //线条样式
    CGContextSetLineWidth(context, 1); //线条宽度
    //    CGContextSetRGBStrokeColor(context, 0.286, 0.776, 0.847, 1.00);
    CGContextSetRGBStrokeColor(context, 0.134f, 0.655f, 1.000f, 1);
//    [UIColor colorWithRed:0.134 green:0.655 blue:1.000 alpha:1.000];
        CGContextBeginPath(context);
        for (int i = 0; i < _arrayIndex; i++) {
            offset+=2;
            _offsetNum = [NSNumber numberWithFloat:offset];
            NSNumber *waveNum = _waveArray[i];
            float wave = waveNum.floatValue / 100 *self.frame.size.height;
            CGContextMoveToPoint(context, offset, self.frame.size.height / 2 + wave * 0.8);
            CGContextAddLineToPoint(context, offset, self.frame.size.height / 2 - wave * 1.2);
        }
        CGContextStrokePath(context);
}



@end
